package cgl.narada.service.security.kmc.messages;

import java.io.IOException;
import java.io.Serializable;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;

import org.apache.log4j.Logger;

import cgl.narada.service.security.kmc.TopicRights;
import cgl.narada.util.ObjectMarshaller;

/**
 * Request for a security key. In this request we present the Topic sysnopsis
 * for which we desire the secret key, the synopsis type, the client's
 * credentials and a response topic <br>
 * Created on Sep 8, 2005
 * 
 * @author Harshawardhan Gadgil (hgadgil@grids.ucs.indiana.edu)
 */
public class SecureTopicKeyRequest implements Serializable {

    static Logger log = Logger.getLogger("SecureTopicKeyRequest");

    /** Topic Synopsis */
    private String topicSynopsis;

    /** Client's credentials */
    private Certificate credentials;

    /** Requested rights */
    private TopicRights requestedRights;

    /** Response Topic */
    private String responseTopic;

    public SecureTopicKeyRequest(String synopsis, Certificate creds,
            TopicRights reqRights, String resp) {
        topicSynopsis = synopsis;
        credentials = creds;
        requestedRights = reqRights;
        responseTopic = resp;
    }

    /**
     * Marshalls the object into an array of bytes.
     * 
     * @return an array of bytes representing the marshaller object, null if
     *         error occurs
     */
    public byte[] getBytes() {
        try {
            return ObjectMarshaller.marshall(this);
        } catch (IOException e) {
            log.error("", e);
            return null;
        }
    }

    /**
     * Unmarshalls the bytes as a SecureTopicKeyRequest object
     * 
     * @param objectBytes
     * @return SecureTopicKeyRequest if successful, null if error occurs
     */
    public static SecureTopicKeyRequest createObjectFromBytes(byte[] objectBytes) {
        try {
            return (SecureTopicKeyRequest) ObjectMarshaller
                    .unmarshall(objectBytes);
        } catch (IOException e) {
            log.error("", e);
        } catch (ClassNotFoundException e) {
            log.error("", e);
        }
        return null;
    }

    /**
     * For debugging purposes...
     */
    public String toString() {
        String ret = "";
        ret += "\nRequestor: "
                + ((X509Certificate) credentials).getSubjectDN().toString()
                + "\n";
        ret += "RequestedRights: " + requestedRights.toString() + "\n";
        ret += "ResponseTopic: " + responseTopic;
        return ret;
    }

    /**
     * @return Returns the credentials.
     */
    public Certificate getCredentials() {
        return credentials;
    }

    /**
     * @return Returns the requestedRights.
     */
    public TopicRights getRequestedRights() {
        return requestedRights;
    }

    /**
     * @return Returns the responseTopic.
     */
    public String getResponseTopic() {
        return responseTopic;
    }

    /**
     * @return Returns the topicSynopsis.
     */
    public String getTopicSynopsis() {
        return topicSynopsis;
    }

}
